-- gain_apprentice
-- Created by chengb Sep/11/2015
-- 成就：获得冈布奥工匠
-- 主要逻辑交给服务器处理（验证客户端会导致无法完成成就）

-- 内部函数声明
local whenAttribBonus;

-- 注册激活试剂的回调
EventMgr.register("game_achievement/gain_apprentice", event.BONUS_ATTRIB, function(args)
    whenAttribBonus(args);
end);

-- 注册激活试剂的回调
whenAttribBonus = function(args)
    local attrib = args["attrib"];
    if attrib ~= "idle_worker_num" then
        -- 不是工匠，不处理
        return;
    end

    local count = args["count"];

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        return true;
    end

    -- 遍历所有的子规则
    local ids = GameAchievementM.getIdsByRule("gain_apprentice");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameAchievementM.tryComplete(id, count);
        end
    end
end

return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = GameAchievementM.query(id);

        local ruleArg  = config["rule_arg"];
        local progress = config["progress"];
        local desc     = config["desc"];

        desc = string.gsub(desc, "{0}", progress);

        return desc;
    end,
};